#!/usr/bin/env python
# -*- coding: utf8 -*-

# Alexandre Coster <acoster@rhrk.uni-kl.de>
# AG DBIS - Technische Universität Kaiserslautern

import sys
import sxml.shaken
from optparse import OptionParser


def main():
    parser = OptionParser(version="1.0", description="sXML (Shaky XML generator) is an application to inject error on the structure of XML files.")

    parser.add_option("-c", "--conf",   dest="conf",   help="Uses FILE as the configuration file",           metavar="FILE")
    parser.add_option("-i", "--input",  dest="input",  help="Applies the transformations to the FILE file.", metavar="FILE")
    parser.add_option("-o", "--output", dest="output", help="Writes the transformed XML file to FILE. This option can be used multiple times, to generate several (differente) files.", metavar="FILE", action="append")
    parser.add_option("-r", "--cfile",  dest="cfile",  help="Outputs the partial error count to FILE. Use stdout or stderr to output to them. This option is deactivated when multiple files are used.", metavar="FILE")
    parser.add_option("", "--no-psyco", dest="use_psyco", action="store_false", help="Disables psyco usage.")

    parser.set_defaults(cfile=None, use_psyco=True)

    (options, args) = parser.parse_args()

    if options.output == None or options.conf == None or options.input == None:
        parser.print_help()
        sys.exit(0)

    if options.use_psyco:
        try:
            import psyco
        except ImportError:
            pass
        else:
            psyco.full()

    sxmlObj = sxml.shaken.ShakenXML(options.conf, options.input, options.cfile)
    sxmlObj.run(options.output)

if __name__ == "__main__":
    main()
